package com.artezio.buyndrive.service.impl;

import com.artezio.buyndrive.dao.Dao;
import com.artezio.buyndrive.service.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

public abstract class AbstractService<T extends Object, D extends Dao<T>> implements Service<T> {

    protected D dao;

    @Transactional(readOnly = true)
    @Override
    public T findById(Long id/*, String... properties*/) {
        return getDao().findById(id/*, properties*/);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> findByIds(List<Long> ids, String... properties) {
        return getDao().findByIds(ids, properties);
    }

    @Transactional
    @Override
    public Long add(T entity) {
        return dao.add(entity);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        T entity = dao.findById(id);
        if (entity != null) {
            dao.delete(entity);
        }
    }

    @Transactional
    @Override
    public void delete(T entity) {
        dao.delete(entity);
    }

    @Transactional
    @Override
    public List<T> getAll() {
        return dao.getAll();
    }

    @Transactional(readOnly = false)
    @Override
    public void saveOrUpdate(T entity) {
        dao.saveOrUpdate(entity);
    }

    public D getDao() {
        return dao;
    }

    public abstract void setDao(D dao);
}
